package com.wtpaint.algorithm.sort;

import com.wtpaint.algorithm.SortUtil;

public class BubbleSort {

    public static void sort(Comparable[] arr) {
        int n = arr.length;
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - 1 - i; j++) {
                if (arr[j].compareTo(arr[j + 1]) > 0) {
                    swap(arr, j, j + 1);
                }
            }
        }
    }

    private static void swap(Object[] arr, int i, int j) {
        Object t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    }

    public static void main(String[] args) {
        int n = 10000;
        Integer[] arr = SortUtil.generateRandomArray(n, 0, n);
        SortUtil.printArray(arr);
        SortUtil.testSort("com.wtpaint.algorithm.sort.BubbleSort", arr);
        SortUtil.printArray(arr);
    }
}
